{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "学习目标\n",
    "- 应用crosstab和pivot_table实现交叉表与透视表"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4411df5eea4ed9ff"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 1 交叉表与透视表什么作用\n",
    "探究股票的涨跌与星期几有关？\n",
    "以下图当中表示，week代表星期几，1,0代表这一天股票的涨跌幅是好还是坏，里面的数据代表比例\n",
    "可以理解为所有时间为星期一等等的数据当中涨跌幅好坏的比例\n",
    "\n",
    "- 交叉表：交叉表用于计算一列数据对于另外一列数据的分组个数(用于统计分组频率的特殊透视表)\n",
    "    - pd.crosstab(value1, value2)\n",
    "- 透视表：透视表是将原有的DataFrame的列分别作为行索引和列索引，然后对指定的列应用聚集函数\n",
    "    - data.pivot_table(）\n",
    "    - DataFrame.pivot_table([], index=[])\n",
    "# 2 案例分析\n",
    "## 2.1 数据准备\n",
    "- 准备两列数据，星期数据以及涨跌幅是好是坏数据\n",
    "- 进行交叉表计算\n"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "a23ab2a54b617f32"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "posi_neg   0   1\nweek            \n0         63  62\n1         55  76\n2         61  71\n3         63  65\n4         59  68",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>posi_neg</th>\n      <th>0</th>\n      <th>1</th>\n    </tr>\n    <tr>\n      <th>week</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>63</td>\n      <td>62</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>55</td>\n      <td>76</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>61</td>\n      <td>71</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>63</td>\n      <td>65</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>59</td>\n      <td>68</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "# 寻找星期几跟股票张得的关系\n",
    "# 1、先把对应的日期找到星期几\n",
    "data = pd.read_csv(\"stock_day.csv\")\n",
    "date = pd.to_datetime(data.index).weekday\n",
    "data['week'] = date\n",
    "\n",
    "# 2、假如把p_change按照大小去分个类0为界限\n",
    "data['posi_neg'] = np.where(data['p_change'] > 0, 1, 0)\n",
    "\n",
    "# 通过交叉表找寻两列数据的关系\n",
    "count = pd.crosstab(data['week'], data['posi_neg'])\n",
    "count"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T11:06:07.794586200Z",
     "start_time": "2024-02-22T11:06:07.781439300Z"
    }
   },
   "id": "a0415c7041bf15b6",
   "execution_count": 3
  },
  {
   "cell_type": "markdown",
   "source": [
    "但是我们看到count只是每个星期日子的好坏天数，并没有得到比例，该怎么去做？\n",
    "- 对于每个星期一等的总天数求和，运用除法运算求出比例"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "efa506f03680612f"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "posi_neg         0         1\nweek                        \n0         0.504000  0.496000\n1         0.419847  0.580153\n2         0.462121  0.537879\n3         0.492188  0.507812\n4         0.464567  0.535433",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>posi_neg</th>\n      <th>0</th>\n      <th>1</th>\n    </tr>\n    <tr>\n      <th>week</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0.504000</td>\n      <td>0.496000</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0.419847</td>\n      <td>0.580153</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.462121</td>\n      <td>0.537879</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>0.492188</td>\n      <td>0.507812</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>0.464567</td>\n      <td>0.535433</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 算数运算，先求和\n",
    "sum = count.sum(axis=1).astype(np.float32)\n",
    "\n",
    "# 进行相除操作，得出比例\n",
    "pro = count.div(sum, axis=0)\n",
    "pro"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T11:07:25.780235300Z",
     "start_time": "2024-02-22T11:07:25.777723Z"
    }
   },
   "id": "95b6b3b755ae859f",
   "execution_count": 5
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.2 查看效果\n"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "92fd4aa355224623"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "<Figure size 640x480 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAiMAAAGrCAYAAAASIZeZAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/H5lhTAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAmIklEQVR4nO3df1iUdb7/8dcMP0XDX+QgBItW/iATFJLF1pXcKawOW57d1tUMl2O6mpw0NM0yyK3EzibROVmURrj71dW2q3Xd1UvrcBY3j5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atWuXZ1u12y+Vy6dChQzp48KCcTqcGDBhQ7/Vra2vVu3dvz8/nzp3TqFGjNHHiROXm5rbWrwmgBREjAK5JUlKS8vPztWvXLvn5+WnQoEFKSkpSUVGRvvzyS88XEH799df65S9/qUcfffSy14iMjNTu3bvl4+OjkpIS+fj41Hu+W7dunv8eEBAgu92uP//5z3r88ccVHh7esr8ggBZHjAC4Jt9eN/LSSy95wiMpKUlLlizRl19+qTlz5kiShg8frk8//VQ33XRTg68zbNgwOZ1OVVZWatSoUY3uz2q16re//a0mTpyoO+64Q0VFRfXe1gHQ/nABK4Br0rNnTw0dOlSrVq1SUlKSJOmHP/yhSktLtX//fk+gzJ8/X9u2bVN6errKysp04MAB/fGPf/RcwDpgwAA9+OCDSk1N1bvvvqtDhw5p+/btys7O1oYNG+rt08fHR6tWrVJMTIzGjBkjh8PRqr8zgOZFjAC4ZqNHj5bT6fTESK9evRQdHa3Q0FANHDhQkjR06FBt2bJF+/fv16hRozRs2DBlZmbWO6vx1ltvKTU1VXPmzNHAgQN1//336+OPP1ZkZORl+/T19dXvfvc73XLLLRozZowqKytb5XcF0Py4mwYAABjFmREAAGAUMQIAAIwiRgAAgFHECAAAMIoYAQAARhEjAADAKGIEAAAYRYwAAACjiBEAAGAUMQIAAIwiRgAAgFH/H2EAbNgTBMypAAAAAElFTkSuQmCC"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "pro.plot(kind='bar', stacked=True)\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T11:08:33.767604500Z",
     "start_time": "2024-02-22T11:08:33.301761400Z"
    }
   },
   "id": "78eefbc2f6697e17",
   "execution_count": 6
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.3 使用pivot_table实现\n",
    "使用透视表，刚才的过程更加简单"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "7d68b54e5ca00a50"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "      posi_neg\nweek          \n0     0.496000\n1     0.580153\n2     0.537879\n3     0.507812\n4     0.535433",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>posi_neg</th>\n    </tr>\n    <tr>\n      <th>week</th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0.496000</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0.580153</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.537879</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>0.507812</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>0.535433</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过透视表，将整个过程变成更简单一些\n",
    "data.pivot_table(['posi_neg'], index='week')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T11:09:18.706188100Z",
     "start_time": "2024-02-22T11:09:18.703672900Z"
    }
   },
   "id": "894b8d9cf879234b",
   "execution_count": 7
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 3 小结\n",
    "- 交叉表与透视表的作用【知道】\n",
    "    - 交叉表：计算一列数据对于另外一列数据的分组个数\n",
    "    - 透视表：指定某一列对另一列的关系"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "1495a719d8ca2fa8"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
